#include <stdio.h>  
#include <stdlib.h>  
  
#define MAX_VERTICES 100  
  
// 定义图的结构体  
typedef struct {  
    int n;
    int matrix[MAX_VERTICES][MAX_VERTICES];
    int adj_list[MAX_VERTICES][MAX_VERTICES];
} Graph;  
  
// 初始化图  
void init_graph(Graph* g, int n) {  
    g->n = n;  
    for (int i = 0; i < n; i++) {  
        for (int j = 0; j < n; j++) {  
            g->matrix[i][j] = 0;  
            g->adj_list[i][j] = 0;  
        }  
    }  
}  
  
// 添加边  
void add_edge(Graph* g, int u, int v) {  
    g->matrix[u][v] = 1;  
    g->matrix[v][u] = 1;
    g->adj_list[u][v] = 1;
    g->adj_list[v][u] = 1; 
}  
  
// 输出邻接矩阵存储的图  
void print_matrix(Graph* g) {  
    printf("邻接矩阵存储的图：\n");  
    for (int i = 0; i < g->n; i++) {  
        for (int j = 0; j < g->n; j++) {  
            printf("%d ", g->matrix[i][j]);  
        }  
        printf("\n");  
    }  
}  
  
// 输出邻接表存储的图  
void print_adj_list(Graph* g) {  
    printf("邻接表存储的图：\n");  
    for (int i = 0; i < g->n; i++) {  
        printf("顶点%d：", i);  
        for (int j = 0; j < g->n; j++) {  
            if (g->adj_list[i][j] == 1) {  
                printf("%d ", j);  
            }  
        }  
        printf("\n");  
    }  
}  
  
int main() {  
    Graph g;  
    init_graph(&g, 5);
    add_edge(&g, 0, 1);
    add_edge(&g, 0, 4);
    add_edge(&g, 1, 2); 
    add_edge(&g, 1, 3);
    add_edge(&g, 1, 4);
    add_edge(&g, 2, 3);
    add_edge(&g, 3, 4);
    print_matrix(&g);
    print_adj_list(&g);
}